package com.chidopi.market.aa.dao.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import com.chidopi.cdf.dao.AbstractBaseDaoImpl;
import com.chidopi.market.aa.domain.Accounts;
import com.chidopi.market.aa.domain.Functions;
import com.chidopi.market.aa.domain.Role2FunctionItem;
import com.chidopi.market.aa.domain.Roles;


public class RoleDaoImpl extends AbstractBaseDaoImpl<Roles> {
	private static Logger logger = LoggerFactory.getLogger(RoleDaoImpl.class);

	public List<Roles> find(String sqlWhere, Object... args) {
		if (sqlWhere == null || sqlWhere.trim().equals("")) {
			String sql = "select * from roles ";
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper());
		} else {
			logger.debug("sqlWhere:[" + sqlWhere + "]/params:" + Arrays.asList(args).toString());
			String sql = "select * from roles where " + sqlWhere;
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper(), args);
		}
	}

	public Roles findById(Object id) {
		String sql = "id=?";
		List<Roles> list = this.find(sql, id);
		if (0 == list.size()) return null;
		return list.get(0);
	}

	public int insert(Roles t) {
		String sql = "insert into roles (rolename, ENABLE_FLAG, CREATE_DATE, CREATE_BY) "
				+ "values (:rolename, :enable_flag, NOW(), :create_by)";
		int ret = super.insert(sql, t);
		
		if (ret > 0) {
			t.setId(ret);
		    updateDetails(t);
		}
		return ret;
	}

	public int update(Roles t) {
		String sql = "update roles set rolename = :rolename, enable_flag = :enable_flag, update_by = :update_by where id= :id";
		int ret = super.execSQL(sql, t);
		if (ret > 0) this.updateDetails(t);
		return ret;
	}

	public int delete(Roles t) {
		logger.debug("Delete:" + t.getId());
		String sql = "delete from roles where id = :id";
		return super.execSQL(sql, t);
	}

	@Override
	public String getSequenceName() {
		return "Roles_SEQ";
	}

	@Override
	public String getTable() {
		return "Roles";
	}

	@Override
	protected RowMapper<Roles> getRowMapper() {
		return ParameterizedBeanPropertyRowMapper.newInstance(Roles.class);
	}

	private void updateDetails(Roles t) {
		// Account2Role
		String sql = null;
		List<Accounts> accounts = t.getAccounts();
		if (null != accounts) {
			sql = "delete from account2role where role_id=?";
			super.simpleJdbcTemplate.update(sql, t.getId());
			sql = "insert into account2role (account_id, role_id) values (?, ?)";
			for (Accounts account : accounts) {
				super.simpleJdbcTemplate.update(sql, account.getId(), t.getId());
			}
		}
		// Role2Function
		List<Functions> functions = t.getFunctions();
		if (functions != null) {
			sql = "delete from role2function where role_id=?";
			super.simpleJdbcTemplate.update(sql, t.getId());
			sql = "insert into role2function (role_id, function_id) values (?, ?)";
			for (Functions function : functions) {
				super.simpleJdbcTemplate.update(sql, t.getId(), function.getId());
			}
		}
		// Role2FunctionItem
		List<Role2FunctionItem> items = t.getRole2functionitem();
		if (items != null) {
			sql = "delete from role2functionitem where role_id=?";
			super.simpleJdbcTemplate.update(sql, t.getId());
			sql = "insert into role2functionitem (role_id, item_id, visable, enable) values (?, ?, ?, ?)";
			for (Role2FunctionItem item : items) {
				logger.debug(item.getRole_id() + "," + item.getItem_id() + "," +  item.getVisable() + "," +  item.getEnable());
				super.simpleJdbcTemplate.update(sql, item.getRole_id(), item.getItem_id(), item.getVisable(), item.getEnable());
			}
		}
	}
}
